package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.business.service.INotificationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.business.domain.BusinessReminder;
import com.ruoyi.business.service.IBusinessReminderService;

/**
 * 提醒管理 信息操作处理
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/business/reminder")
public class ReminderController extends BaseController {
    private String prefix = "business/reminder";

    @Autowired
    private IBusinessReminderService businessReminderService;

    @Autowired
    INotificationService notificationService;

    @RequiresPermissions("business:reminder:view")
    @GetMapping()
    public String reminder() {
        return prefix + "/reminder";
    }

    /**
     * 查询提醒列表
     */
    @RequiresPermissions("business:reminder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusinessReminder businessReminder) {
        startPage();
        List<BusinessReminder> list = businessReminderService.selectBusinessReminderList(businessReminder);
        return getDataTable(list);
    }

    /**
     * 导出提醒列表
     */
    @RequiresPermissions("business:reminder:export")
    @Log(title = "提醒管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusinessReminder businessReminder) {
        List<BusinessReminder> list = businessReminderService.selectBusinessReminderList(businessReminder);
        ExcelUtil<BusinessReminder> util = new ExcelUtil<BusinessReminder>(BusinessReminder.class);
        return util.exportExcel(list, "提醒数据");
    }

    /**
     * 下载导入模板
     */
    @RequiresPermissions("business:reminder:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<BusinessReminder> util = new ExcelUtil<BusinessReminder>(BusinessReminder.class);
        return util.importTemplateExcel("提醒数据模板");
    }

    /**
     * 导入提醒数据
     */
    @RequiresPermissions("business:reminder:import")
    @Log(title = "提醒管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<BusinessReminder> util = new ExcelUtil<BusinessReminder>(BusinessReminder.class);
        List<BusinessReminder> reminderList = util.importExcel(file.getInputStream());
        String operName = getLoginName();
        String message = businessReminderService.importReminder(reminderList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 新增提醒
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存提醒
     */
    @RequiresPermissions("business:reminder:add")
    @Log(title = "提醒管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusinessReminder businessReminder) {
        return toAjax(businessReminderService.insertBusinessReminder(businessReminder));
    }

    /**
     * 修改提醒
     */
    @RequiresPermissions("business:reminder:edit")
    @GetMapping("/edit/{reminderId}")
    public String edit(@PathVariable("reminderId") Long reminderId, ModelMap mmap) {
        BusinessReminder businessReminder = businessReminderService.selectBusinessReminderById(reminderId);
        mmap.put("businessReminder", businessReminder);
        return prefix + "/edit";
    }

    /**
     * 修改保存提醒
     */
    @RequiresPermissions("business:reminder:edit")
    @Log(title = "提醒管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusinessReminder businessReminder) {
        return toAjax(businessReminderService.updateBusinessReminder(businessReminder));
    }

    /**
     * 删除提醒
     */
    @RequiresPermissions("business:reminder:remove")
    @Log(title = "提醒管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(businessReminderService.deleteBusinessReminderByIds(ids));
    }

    /**
     * 手动触发发送提醒
     */
    @RequiresPermissions("business:reminder:trigger")
    @Log(title = "提醒管理", businessType = BusinessType.OTHER)
    @PostMapping("/trigger/{reminderId}")
    @ResponseBody
    public AjaxResult triggerSend(@PathVariable("reminderId") Long reminderId) {
        return toAjax(businessReminderService.triggerSendReminder(reminderId));
    }

    /**
     * 查看提醒详情
     */
    @RequiresPermissions("business:reminder:detail")
    @GetMapping("/detail/{reminderId}")
    public String detail(@PathVariable("reminderId") Long reminderId, ModelMap mmap) {
        BusinessReminder businessReminder = businessReminderService.selectBusinessReminderById(reminderId);
        mmap.put("businessReminder", businessReminder);
        return prefix + "/detail";
    }

    /**
     * 取消提醒任务
     */
    @RequiresPermissions("business:reminder:cancel")
    @Log(title = "提醒管理", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel/{reminderId}")
    @ResponseBody
    public AjaxResult cancel(@PathVariable("reminderId") Long reminderId) {
        return toAjax(businessReminderService.cancelReminder(reminderId));
    }

    /**
     * 发送信息提示
     */
    @RequiresPermissions("business:reminder:edit")
    @Log(title = "提醒企业微信服务", businessType = BusinessType.OTHER)
    @PostMapping("/sendInfoMsg")
    @ResponseBody
    public AjaxResult sendInfoMsg(String reminderId, String mobilePhone, String content) {
        try {
            // 这里可以调用实际的短信发送服务
            // 目前仅作为示例，实际情况需要根据系统的短信服务集成方式实现
            logger.info("发送企业微信信息提示: reminderId={}, mobilePhone={}, content={}", reminderId, mobilePhone, content);

            // 记录发送日志
            // TODO: 可以在这里添加实际的短信发送逻辑和日志记录
//            businessReminderService.sendReminders();
            businessReminderService.sendRemindersWithoutCondition(reminderId);

            return AjaxResult.success("发送成功");
        } catch (Exception e) {
            logger.error("发送信息提示失败", e);
            return AjaxResult.error("发送失败：" + e.getMessage());
        }
    }

    @RequiresPermissions("business:reminder:edit")
    @Log(title = "提醒短信服务", businessType = BusinessType.OTHER)
    @PostMapping("/sendMobile")
    @ResponseBody
    public AjaxResult sendMobile(String reminderId, String mobilePhone, String content) {
        try {
            // 这里可以调用实际的短信发送服务
            // 目前仅作为示例，实际情况需要根据系统的短信服务集成方式实现
            logger.info("发送短信信息提示: reminderId={}, mobilePhone={}, content={}", reminderId, mobilePhone, content);

            // 记录发送日志
            // TODO: 可以在这里添加实际的短信发送逻辑和日志记录

            return AjaxResult.success("发送成功");
        } catch (Exception e) {
            logger.error("发送信息提示失败", e);
            return AjaxResult.error("发送失败：" + e.getMessage());
        }
    }


}